﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace TopologySearch
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdTestLoopOffset : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_uiApp = cmdData.Application;
			this.m_uiDoc = this.m_uiApp.ActiveUIDocument;
			this.m_app = this.m_uiApp.Application;
			this.m_doc = this.m_uiDoc.Document;
			IList<Reference> list = new List<Reference>();
			if (!this.SelectCurves(ref list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			List<Curve> list2 = new List<Curve>();
			foreach (Reference reference in list)
			{
				Element element = this.m_doc.GetElement(reference);
				if (element.GetType() == typeof(Wall))
				{
					Wall wall = element as Wall;
					list2.Add(((LocationCurve)wall.Location).Curve);
				}
				else if (element.GetType() == typeof(ModelLine))
				{
					ModelLine modelLine = element as ModelLine;
					list2.Add(modelLine.GeometryCurve);
				}
				else if (element.GetType() == typeof(ModelArc))
				{
					ModelArc modelArc = element as ModelArc;
					list2.Add(modelArc.GeometryCurve);
				}
				else if (element.GetType() == typeof(DetailLine))
				{
					DetailLine detailLine = element as DetailLine;
					list2.Add(detailLine.GeometryCurve);
				}
				else if (element.GetType() == typeof(DetailArc))
				{
					DetailArc detailArc = element as DetailArc;
					list2.Add(detailArc.GeometryCurve);
				}
			}
			List<Pole2D> list3 = new List<Pole2D>();
			foreach (Curve curve in list2)
			{
				Pole2D item = Pole2D.TransformWallToStructure2DPole(curve, ElementId.InvalidElementId, 0.0, 0.0);
				list3.Add(item);
			}
			StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(cmdData);
			List<List<Curve>> fullFloorEdgeLoops = structureLoopSearcher.GetFullFloorEdgeLoops(list3, StructureLoopSearcher.SearchEdgeLocation.SEL_Center);
			if (fullFloorEdgeLoops.Count < 1)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			OffsetTestForm offsetTestForm = new OffsetTestForm();
			if (offsetTestForm.ShowDialog() != DialogResult.OK)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			List<List<Curve>> list4 = new List<List<Curve>>();
			if (object.Equals(offsetTestForm.Offset, 0.0))
			{
				list4 = fullFloorEdgeLoops;
			}
			else
			{
				list4 = structureLoopSearcher.OffsetLoop(fullFloorEdgeLoops[0], offsetTestForm.Offset);
			}
			CurveArray curveArray = new CurveArray();
			foreach (List<Curve> list5 in list4)
			{
				foreach (Curve curve2 in list5)
				{
					curveArray.Append(curve2);
				}
			}
			if (curveArray.Size > 0)
			{
				Transaction transaction = new Transaction(this.m_doc);
				transaction.Start("draw loop");
				this.m_doc.Create.NewDetailCurveArray(this.m_uiDoc.ActiveView, curveArray);
				transaction.Commit();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool SelectCurves(ref IList<Reference> selRefs)
		{
			bool result;
			try
			{
				selRefs = this.m_uiDoc.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new SearchDataFilter(), "请选择墙或参考线：");
				result = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		private UIApplication m_uiApp;

		private UIDocument m_uiDoc;

        private Autodesk.Revit.ApplicationServices.Application m_app;

		private Document m_doc;
	}
}
